package model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import controller.LastAddedController;

/**
 * Order the contacts of the array by date of last added
 * @author Marie Morin
 *
 */
public class ByLastAddedModel{

	private LastAddedController c;
	private ArrayList<Contact> newContactsList= new ArrayList<Contact>();
	private ArrayList<Date> newDateList= new ArrayList<Date>();	
	private String text;
	
	
	public ByLastAddedModel(LastAddedController c){

		this.c = c;
		
		if (c.getContactsList().size()>0){
	 	        	
 	        	 this.orderDates();
 
			}
			
			this.c.setContactsList(newContactsList);	
	
		
	}
	
	/**
	 * Order the contact of the list by date (last added contact to first added contact)
	 * 
	 */
	public void orderDates(){

    	Contact b = this.c.getContactsList().get(0);
    	int size = this.c.getContactsList().size();
    	int s = this.c.getContactsList().size();
    	int a =0;
 
		
		
    	
    	for (int i=0; i<s-1; i++){
    		
    		b = this.c.getContactsList().get(0);
    		a=0;
    	
    		for (int j=1; j<size; j++){
    				
    			if (b.getDate().compareTo(this.c.getContactsList().get(j).getDate())<0)
    			{
    				b = this.c.getContactsList().get(j);
    				a=j;
    			}

    		
    		}

    		
    		newContactsList.add(b);
    		c.getContactsList().remove(a);
    		size--;

    	
    	}
    	
    	newContactsList.add(c.getContactsList().get(0));
    	
	}





}
